↜ Back to index Introduction to Numerical Analysis 2

Lecture a3

Arrays in Fortran

An array variable can store a large number of values of the same type.

Array variables can be declared by adding the number of elements in parentheses:

implicit none
integer i
real a(5)       ! declares an array of 5 real values a(1), a(2), ..., a(5)
integer b(-4:3) ! declares an array of 8 integer values b(-4), b(-3), ..., b(3)

! assign a single value
a(1) = 3

! use a loop to initialize all values
do i = 1,5
    a(i) = i
end do

do i = -4,3
    b(i) = i**2
end do

! print a single value
print *, a(2)

! print all the values
print *, a
print *, b

end

Arrays can be also initialized using a special syntax (/ ... /):

implicit none
real a(5)    

a = (/ 1., 5., 0.5, -1., 2. /)

print *, a(2)

end

This is only used in practice for small arrays. Large arrays are initialized by do loops.

Always initialize the values in an array before you use them.

Allowed index values

Question. What happens if we try to access elements like a(0), a(6), a(1000) in the above code?

Answer. Accessing elements out of the declared range is undefined in Fortran so Fortran can do anything. It might look like it works fine or it might crash the program. Often it looks OK but the computation results are wrong.

Danger!! It is illegal to access elements with index out of bounds. In the above code, a(0), a(6) or a(1000) are illegal. Unfortunately, by default, this is is sometimes reported as a warning but it can lead to very hard-to-find bugs.

implicit none
integer i
real a(5)       ! declares an array of 5 real values a(1), a(2), ..., a(5)

! oops, index out of bounds; warning in my gfortran
a(0) = 5

i = 0
! oops, index out of bounds, but no warning!!
a(i) = 1

end

Suggestion. Enable index checks by -fcheck=bounds when using arrays to get a runtime check of all array accesses:

gfortran -fcheck=bounds array.f90

Array size as a named constant

It is convenient to define the size of the array as a named constant so that it can be easily changed.

implicit none
integer, parameter :: n = 5
integer i
real a(n)

! initialize all elements
do i=1,n
    a(i) = i
end do

! print the last element
print *, a(n)

end

Allocatable arrays

So far we have been using arrays whose size is determined at the compile time. However, it is possible to set the size during runtime, for example based on users output. This is what Fortran’s allocatable arrays are for.

implicit none
real, dimension(:), allocatable :: x, y
integer n, i

! we cannot use arrays x, y yet since they are not allocated

print *, 'Enter array size'
read *, n

! allocate the memory for n elements
allocate(x(n))
! allocate the memory for n + 1 elements
allocate(y(0:n))

! now we can start using x as any other array with elements x(1), x(2), ..., x(n)
do i = 1, n
    x(i) = i
enddo

print *, x

do i = 0, n
    y(i) = i
enddo

print *, y

end

Exercises

Exercise 1.

  1. Write a program that initializes all the elements of an array so that the element a(i) of the array has the value \min(i^2 - 100, 100 - i).

    The array should be declared as

    integer, parameter :: n = 100
    real a(n)

  2. Write a program that doubles (multiplies by 2) each element of the array in part 1.

    Example. For array defined as in

    real a(5)    
    a = (/ 1., 5., -1., 2., -3. /)

    the program should end up with array a that is equal to

    (/ 2., 10., -2., 4., -6. /)

Exercise 2.

  1. Write a program that computes the sum of elements of the array in Exercise 1.

  2. Use 1. to compute the average of the elements of the array: \sum_{i=1}^n a_i \over n

  3. Compute the variance by the formula \sum_{i=1}^n a_i^2 - (\sum_{i=1}^n a_i)^2/n\over n

Exercise 3.

  1. Write a program that finds the largest value (the maximum) of the array from Exercise 1.

    Example. For array defined as in

    real a(5)    
    a = (/ 1., 5., -1., 2., -7. /)

    the program will output 5.00000.

  2. Find the smallest value (the minimum).

    Example. For array in 1. the program will output -7.00000.

  3. Find the maximum of the absolute value of the elements of the array. In mathematics, this is called the maximum norm (最大値ノルム).

    Example. For array in 1. the program will output 7.00000.

Exercise 4. It is often very useful to find the index of an element that satisfies some condition. This is called searching, finding a position, …

  1. Write a program that finds the smallest index i such that a(i) > 0. is true. Again, use the array from Exercise 1 to test the code.

    Example. For array defined as in

    real a(5)    
    a = (/ -1., -5., 1., 2., -3. /)

    the program would output 3.

  2. What should be the answer of the program be if the condition a(i) > 100 is used instead?

Exercise 5. Write a program that finds the first index of the element that has the largest value.

Example. For array defined as in

real a(5)    
a = (/ 1., 5., 0.5, -1., 5. /)

the program would output 2.

Exercise 6. Write a program that sorts the array in Exercise 1 from smallest to largest.

Example. For array defined as in

real a(5)    
a = (/ 1., 5., -1., 2., -3. /)

the program should end up with array a that is equal to

(/ -3., -1., 1., 2., 5. /)